package top.infopub.mgr.bpa.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.dto.bpa.PrimarySureDto;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.bpa.dao.*;
import top.infopub.mgr.bpa.domain.IncaeSureDomain;
import top.infopub.mgr.bpa.service.IncaeSureService;
import top.infopub.mgr.bpa.vo.IncaeSureVo;
import top.infopub.mgr.flow.cnst.FlowTemplateEnum;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.util.ReflectBeanUtils;
import top.infopub.model.bpa.*;

import java.util.Date;
import java.util.List;

@Service
public class IncaeSureServiceImpl extends GenericServiceImpl<IncaeSure, String> implements
		IncaeSureService {

	@Autowired
	private IncaeSureDao incaeSureDao;
	
	@Autowired
	private IncaeSureAttDao incaeSureAttDao;
	
	@Autowired
	private IncaeSureDetailDao incaeSureDetailDao;
	
	@Autowired
	private IncaeSureSubDao incaeSureSubDao;
	
	@Autowired
	private IncaeSureListDao incaeSureListDao;
	
	@Autowired
    private WsFlowProcessService wsFlowProcess;


    @Override
	public GenericDao<IncaeSure, String> getDao() {
		return incaeSureDao;
	}

    @Override
    public Page<IncaeSureDomain> selectByDomainWhere(Page<IncaeSureDomain> page,
                                                     IncaeSureVo incaeSureVo) {
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        return (Page<IncaeSureDomain>)incaeSureDao.selectByDomainWhere(incaeSureVo);
    }

    @Override
    public Page<IncaeSureDomain> selectMyCheckByDomainWhere(Page<IncaeSureDomain> page,
                                                            IncaeSureVo incaeSureVo) {
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        return (Page<IncaeSureDomain>)incaeSureDao.selectMyCheckByDomainWhere(incaeSureVo);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void saveIncaeSure(IncaeSure incaeSure, List<IncaeSureAtt> incaeSureAtts,
                              List<IncaeSureList> incaeSureLists,
                              List<IncaeSureSub> incaeSureSubs,
                              List<IncaeSureDetail> incaeSureDetails) {
        //添加主表
        incaeSureDao.insertById(incaeSure);

        if(CollectionUtils.isNotEmpty(incaeSureSubs)){
            incaeSureSubDao.insertByBatch(incaeSureSubs);
        }
        
        if(CollectionUtils.isNotEmpty(incaeSureDetails)){
            incaeSureDetailDao.insertByBatch(incaeSureDetails);
        }
        
        if(CollectionUtils.isNotEmpty(incaeSureLists)){
            incaeSureListDao.insertByBatch(incaeSureLists);
        }
        
        if(CollectionUtils.isNotEmpty(incaeSureAtts)){
            incaeSureAttDao.insertByBatch(incaeSureAtts);
        }
        
        // 注意 这里根据不同的业务分支判断条件 获得不同enum
        FlowTemplateEnum templateEnum = FlowTemplateEnum.BPA_INCAE_SURE;
        // 处理工作流数据
        wsFlowProcess.launch(templateEnum, incaeSure.getId(), incaeSure.getProcessId(), null);
        
    }

    @Override
    public IncaeSureVo queryById(String id) {
        return incaeSureDao.queryById(id);
    }

    @Override
    public String insertById(PrimarySureDto primarySureDto, String creator) {
        IncaeSureVo sureVo = ReflectBeanUtils.do2bo(primarySureDto, IncaeSureVo.class);
        Date date = new Date();
        sureVo.setAddBy(creator);
        sureVo.setAddTime(date);
        sureVo.setUptBy(creator);
        sureVo.setUptTime(date);
        incaeSureDao.insertSelective(sureVo);
        return sureVo.getId();
    }

    @Override
    public void updateByIdSelective(PrimarySureDto primarySureDto, String modifier) {
        IncaeSureVo sureVo = ReflectBeanUtils.do2bo(primarySureDto, IncaeSureVo.class);
        sureVo.setId(primarySureDto.getId());
        Date date = new Date();
        sureVo.setUptBy(modifier);
        sureVo.setUptTime(date);
        incaeSureDao.updateByIdSelective(sureVo);
    }

}